package org.example.c4;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author liu-rui
 * @date 2020/6/8 下午4:42
 * @description 性能对比AtomicInteger和LongAdder
 * @since
 */
public class Demo7 {
    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                // 导入要测试的类
                .include(Demo7.class.getName())
                // 预热5轮
                .warmupIterations(5)
                // 度量10轮
                .measurementIterations(10)
                .mode(Mode.Throughput)
                .threads(8) //默认是１，多个线程才能体现出LongAdder优势
                .forks(3)
                .build();

        new Runner(opt).run();
    }

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    @Benchmark
    public void testAtomicInteger() {
        atomicInteger.incrementAndGet();
    }

    private static LongAdder longAdder = new LongAdder();

    @Benchmark
    public void testLongAdder() {
        longAdder.add(1);
    }
}
